package com.gps.web.dao.sheet;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gps.web.comm.dao.BaseDaoBean;
import com.gps.web.entity.sheets.MaintainRecord;

@Repository
public class MaintainRecordDaoBean extends BaseDaoBean<MaintainRecord, Integer>
		implements MaintainRecordDao {

	@Override
	@Transactional
	public List<MaintainRecord> getEntityByPage(Map<String, Object> conditions) {
		return findByCriteria(getCriteria(conditions));
	}

	private Criteria getCriteria(Map<String, Object> conditions) {
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(MaintainRecord.class);

		if (!StringUtils.isBlank((String) conditions.get("corpName"))) {
			criteria.add(Restrictions.like("corpName",
					conditions.get("corpName").toString(), MatchMode.ANYWHERE));
		}
		if (!StringUtils.isBlank((String) conditions.get("maintainType"))) {
			criteria.add(Restrictions.eq("maintainType",
					conditions.get("maintainType").toString()));
		}
		if (!StringUtils.isBlank((String) conditions.get("licensePlateNum"))) {
			criteria.add(Restrictions.eq("licensePlateNum",
					conditions.get("licensePlateNum").toString()));
		}
		if (!StringUtils.isBlank((String) conditions.get("deviceID"))) {
			criteria.add(Restrictions.eq("deviceID",
					conditions.get("deviceID").toString()));
		}
		if (!StringUtils.isBlank((String) conditions.get("carNum"))) {
			criteria.add(Restrictions.eq("carNum",
					conditions.get("carNum").toString()));
		}
		if (!StringUtils.isBlank((String) conditions.get("motorcade"))) {
			criteria.add(Restrictions.like("motorcade",
					conditions.get("motorcade").toString(), MatchMode.ANYWHERE));
		}

		return criteria;
	}
}
